package Stack_04.base;

/*
 * @Author 罗
 * @date 2020/3/13 - 6:50 下午
 *
 */

public class Stack <T>{
    private Object elementData[];
    private int top;
    public void push(T element){
        if(! isFull()){
            top++;
            this.elementData[top] = element;
        }else{
            System.err.println("栈已满，push()失败");
        }

    }
    public T pop(){
        if(isEmpty()){
            System.err.println("栈中数据为空");
            return null;
        }

        T result = (T) elementData[top];
        top--;
        return result;
    }

//    遍历栈，从栈顶开始遍历
    public void show(){
        for (int i = top; i > -1; i--) {
            System.out.println("elementData["+i+"] = "+elementData[i]);
        }
    }

    public boolean isEmpty(){
        return top == -1;
    }

    public boolean isFull(){
        return top == elementData.length - 1;
    }



    public <T>Stack(int size){
        this.elementData = new Object[size];
        top = -1;
    }

    public int size(){
        return elementData.length;
    }
    public int getValidCount(){
        return top;
    }
}
